package zerous.taphero;
import java.io.*;
import android.content.*;
import zerous.taphero.util.Profile.*;
import zerous.taphero.util.*;
import java.util.*;

public class Hero implements Serializable {
	public static final int WARRIOR = 1;
	public static final int WIZARD = 2;
	
	public String name;
	public int exp, level;
	public int needExp;
	public int kind;
	
	public List<Item> storage = new ArrayList<Item>();
	
	public Hero(String name) {
		this.name = name;
	}
	
	public Integer[] getStorageItems() {
		Integer[] st = new Integer[storage.size()];
		int c = 0;
		for(Item i:storage) {
			st[c] = i.data.id;
			c++;
		}
		return st;
	}
	
	public void init() {
		DataGroup p = CharacterSelectActivity.profile.getDataGroup(name);
		exp = p.getData("exp").data;
		level = p.getData("level").data;
		needExp = 10 + (int)Math.round(Math.pow(level, 2))>>2;
		String storageData = ((String)p.getData("storage").data);
		String[] datas = storageData.split("_");
		for (String s:datas) {
			storage.add(Item.createItem(Integer.valueOf(s)));
		}
	}
	
	public void save() {
		DataGroup p = CharacterSelectActivity.profile.getDataGroup(name);
		p.putInt("exp", exp);
		p.putInt("level", level);
		Data<String> storageData = (Data<String>)p.getData("storage");
		for(Item i:storage) {
			storageData.data += i.data.id + "_";
		}
	}
	
	public void addItem(int id) {
		storage.add(Item.createItem(id));
	}
	
	public boolean earnExp(int amount) {
		exp+=amount;
		if (exp > needExp) {
			exp = 0;
			++level;
			needExp = getExpLimit();
			return true;
		}
		return false;
	}
	
	public int getExpLimit() {
		return 10 + (int)Math.round(Math.pow(level, 2))>>2;
	}
}
